package com.entities.userrole.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.protocols.IServiceLayerDelegate;
import com.core.servlets.TaskTrackerServlet;
import com.core.utils.GlobalConstants;
import com.entities.userrole.servicelayer.UserRoleListLayer;

/**
 * Servlet implementation class UserRoleDelete
 */
public class UserRoleDelete
        extends TaskTrackerServlet
        implements IServiceLayerDelegate {

	private static final long   serialVersionUID = 1L;
	private HttpServletResponse postResponse     = null;



	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserRoleDelete() {

		super();

	}



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet ( HttpServletRequest request,
	                       HttpServletResponse response )
	        throws ServletException, IOException {

	}



	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost ( HttpServletRequest request,
	                        HttpServletResponse response )
	        throws ServletException, IOException {

		this.postResponse = response;

		String roleId = request.getParameter(GlobalConstants.SERVLET_PARAM_KEY);
		Integer objectId = null;
		try{
			objectId = Integer.parseInt(roleId);
		}catch(NumberFormatException e){
			objectId = null;
		}

		UserRoleListLayer roleListLayer = new UserRoleListLayer();
		roleListLayer.setLayerDelegate(this);
		roleListLayer.deleteObject(objectId);
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		if ( postResponse == null ) {
			return;
		}

		if ( exception != null ) {

		}

		String redirection = null;
		redirection = String.format("%s", GlobalConstants.SERVLET_ADMINISTRATION);

		try {
			postResponse.sendRedirect(redirection);
		} catch ( IOException e ) {
			e.printStackTrace();
		}

	}

}
